<template>
  <PageWrapper class="high-form" title="归档">

    <CollapseContainer title="基础信息">
      <BasicForm @register="registerForm">
      </BasicForm>
    </CollapseContainer>

    <a-button type="primary" @click="handleSubmitForm" v-if="serverData.showCommit"> 提交 </a-button> <span> </span>
    <a-button type="primary" @click="handleSaveForm(0)" v-if="serverData.showCommit"> 存草稿 </a-button> <span> </span>
  </PageWrapper>
</template>

<script lang="ts">
import { defineComponent, ref, onBeforeMount, onMounted, } from 'vue';
import { Button, Tag } from 'ant-design-vue';
import { BasicForm, useForm, ApiSelect } from '/@/components/Form/index';
import { CollapseContainer } from '/@/components/Container';
import { useMessage } from '/@/hooks/web/useMessage';
import { PageWrapper } from '/@/components/Page';
import { useRoute, useRouter } from 'vue-router'

import SelectApprover from '/@/views/process/SelectApprover.vue';
import { businessdata, workflowdata, } from '/@/api/work/work';
import { formSchema9 } from './data';
import { update, } from '/@/api/project/collect';

export default defineComponent({
  components: { BasicForm, CollapseContainer, PageWrapper, Button, Tag, ApiSelect, SelectApprover, },
  emits: ['success', 'register'],
  setup() {
    //主表单
    const [registerForm, { setFieldsValue, validate }] = useForm({
      schemas: formSchema9,
      labelWidth: 200,
      showActionButtonGroup: false,
    });

    //流程参数 begin
    const serverDataType: any = { showApprove: false, aaList: [], ccList: [] }
    const serverData = ref(serverDataType);
    const id = ref(0);
    const type = ref('Archive');
    function getFlowData(returnData: any) {
      serverData.value = returnData
    }
    //流程参数 end

    onBeforeMount(() => {
      // 获取路由参数
      const r = useRoute();
      let idStr = r.params.id
      if (typeof (idStr) == 'string') {
        id.value = parseInt(idStr, 10)
      } else {
        id.value = parseInt(idStr[0], 10)
      }
    })

    onMounted(() => {
      if (id.value > 0) {
        workflowdata({ id: id.value, type: type.value }).then(res => {
          serverData.value = res
        })
        setFieldsValue(serverData)
        businessdata({ id: id.value, type: type.value }).then(res => {
          setFieldsValue(res)
          console.log('获取【归档】数据成功', res)
        })
      }
    })

    const { createMessage } = useMessage();
    const router = useRouter();

    //提交
    async function handleSubmitForm() {
      handleSaveForm(1).then(() => {
        router.push('/work/work-done');//跳转已处理
      })
    }
    async function handleSaveForm(saveType) {
      let content = '存草稿成功'
      const values = await validate();
      values['formType'] = type.value
      values['isCommit'] = false
      if (saveType === 1) {
        content = '提交成功'
        values['isCommit'] = true
      }

      let delivery = {
        archiveFile: values['delivery.archiveFile'],
        archiveNo: values['delivery.archiveNo'],
        archiveRemark: values['delivery.archiveRemark'],
        id: values['delivery.id'],
      }
      values['delivery'] = delivery

      await update(values);
      createMessage.success({
        content: content,
        duration: 1,
      });
    }

    return {
      registerForm, handleSaveForm, handleSubmitForm,
      getFlowData, serverData, id, type
    };
  },
});
</script>
